package com.demo.gulimall.cart.vo;

import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * 整个购物车
 * @author Chen Yuan
 * @create 2023-06-19  10:15
 */
@Data
public class CartVo implements Serializable {

	private List<CartItemVo> cartItems;
	// 商品总数量
	private Integer countNum = 0;
	// 商品类型数量
	private Integer countType;
	// 所有商品总价
	private BigDecimal totalAmount;
	// 优惠金额
	private BigDecimal reduce = BigDecimal.ZERO;


	public Integer getCountNum() {
		if (null != cartItems && cartItems.size() > 0) {
			countNum = cartItems.stream().mapToInt(CartItemVo::getCount).sum();
		}
		return countNum;
	}

	public Integer getCountType() {
		if (null != cartItems && cartItems.size() > 0) {
			countType = cartItems.size();
		}
		return countType;
	}

	public BigDecimal getTotalAmount() {
		totalAmount = BigDecimal.ZERO;
		if (null != cartItems && cartItems.size() > 0) {
			totalAmount = cartItems.stream().filter(CartItemVo::getCheck)
											.map(CartItemVo::getTotalPrice)
											.reduce(BigDecimal.ZERO, BigDecimal::add);
		}

		totalAmount = totalAmount.subtract(getReduce());

		return totalAmount;
	}

	public BigDecimal getReduce() {
		return reduce;
	}
}
